package com.hynet.customer;

import com.alibaba.fastjson.JSONObject;
import com.hynet.customer.client.common.sm2.SignVO;
import com.hynet.customer.client.common.sm2.Sm2Util;
import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;


/**
 *SM2验签Test
 */

public class Sm2Test {


        /**
         * 异步验签老公钥
         */

    String publicKey0 =("MIIBMzCB7AYHKoZIzj0CATCB4AIBATAsBgcqhkjOPQEBAiEA/////v//////////\n" +
             "//////////8AAAAA//////////8wRAQg/////v////////////////////8AAAAA\n" +
             "//////////wEICjp+p6dn140TVqeS89lCafzl4n1FauPkt28vUFNlA6TBEEEMsSu\n" +
             "LB8ZgRlfmQRGajnJlI/jC7/yZgvhcVpFiTNMdMe8Nzai9PZ3nFm9zuNraSFT0KmH\n" +
             "fMYqR0AC3zLlITnwoAIhAP////7///////////////9yA99rIcYFK1O79Ak51UEj\n" +
             "AgEBA0IABFM7zvtToMT87VpSM4WSmbdKka6OclquSnS57nmN13EL3hxxt3HV3p2c\n" +
             "laOie/UMUDSkXs8mJLXl4EWoznOAoBw=");


    String publicKey3 =("MFkwEwYHKoZIzj0CAQYIKoEcz1UBgi0DQgAEK2X3eSO4M3CSzlJfajjtlpqayQ0DQ0UfSLiAa6APukvaq3OgDnDYOfx5TeNUUGWGCKHJhBRZcB7Nccl/XCr55w==");



     String privateKey =("MIIBYQIBADCB7AYHKoZIzj0CATCB4AIBATAsBgcqhkjOPQEBAiEA/////v////////////////////8AAAAA//////////8wRAQg/////v////////////////////8AAAAA//////////wEICjp+p6dn140TVqeS89lCafzl4n1FauPkt28vUFNlA6TBEEEMsSuLB8ZgRlfmQRGajnJlI/jC7/yZgvhcVpFiTNMdMe8Nzai9PZ3nFm9zuNraSFT0KmHfMYqR0AC3zLlITnwoAIhAP////7///////////////9yA99rIcYFK1O79Ak51UEjAgEBBG0wawIBAQQgC8HB0ncbZLoZItcvikUc0JqCF2902XXUhOxiyGIXa3WhRANCAATrS4u+FeOtlLhRlq3CxvaUQ2s8EzYXD9HarIsQ0riCStqWh8E4+4FZDg9mq5Z4FhcyrA14ZrFp52t0SDKF8rwE");



       /**
         * sm2验签   异步验签
         * 异步通知内容：
         *
         */
        @Test
        public void Sm2verify0() throws Exception {
            //新
                String sign = "f1lkmW+zGmbNCNkka4EDWwyaN4/YQBSxKM+L/sH1YpnHpixkhrHZMXFUIxsWwCAwm4jaogAqc59l1XuJZlFo3w==";
                String waitStr = "app_id=hy20250207154518238161&apply_no=250514111502078236&customer_id=250514111502001536&heepay_account=13788773588139.com&heepay_id=2360319&notify_type=1&reject_reason=1";
                String strUserId = "1234567812345678";
                Boolean flag = Sm2Util.verifySm3WithSm2(waitStr, strUserId, sign, Sm2Util.string2PublicKey(publicKey3));
                System.out.println(flag);
        }


        @Test
        public void Sm2verify02() throws Exception {
            //新
                String sign = "IpDeePtp1OqDigxorexxFNAU77JDH+cbIAJEAvF6nr7+hgeEmxFkmP1LfJaShT9UmBoEDvTwQcHBFPq2oHhMjA==";
                String waitStr = "app_id=hyo221102213123300000012jykjyxgs&apply_no=240415185145098101&customer_id=240415185145001401&heepay_account=13189511996163.com&heepay_id=2218280&notify_type=1";
                String strUserId = "1234567812345678";
                Boolean flag = Sm2Util.verifySm3WithSm2(waitStr, strUserId, sign, Sm2Util.string2PublicKey(publicKey3));
                System.out.println(flag);
        }


        @Test
        public void Sm2verify01() throws Exception {
            //旧
                String sign = "TrSBE1BIHlpIDJwL/cLrnAXseFQt8Yhr+XYaU7Vf3TXOVAjO489PXN2hXMFyKSEGYZuVDV5rgWuoAFR+PcCy9A==";
                String waitStr = "app_id=hykj-sm2002&apply_no=250211155415007661&customer_id=250211155415013561&heepay_account=68706672@sohu.com&heepay_id=2212400&notify_type=-1";
                String strUserId = "1234567812345678";
                Boolean flag = Sm2Util.verifySm3WithSm2(waitStr, strUserId, sign, Sm2Util.string2PublicKey(publicKey0));
                System.out.println(flag);
        }






        /**
         * 同步验签老公钥
         */
        //String publicKey1 =("MIIBMzCB7AYHKoZIzj0CATCB4AIBATAsBgcqhkjOPQEBAiEA/////v////////////////////8AAAAA//////////8wRAQg/////v////////////////////8AAAAA//////////wEICjp+p6dn140TVqeS89lCafzl4n1FauPkt28vUFNlA6TBEEEMsSuLB8ZgRlfmQRGajnJlI/jC7/yZgvhcVpFiTNMdMe8Nzai9PZ3nFm9zuNraSFT0KmHfMYqR0AC3zLlITnwoAIhAP////7///////////////9yA99rIcYFK1O79Ak51UEjAgEBA0IABChf5Gs11hyWHD4Tn0MfyZvHjd9L5XO3xz2cU/hmXb+YcL9lk4xKMC+VZ0JEx6Pm/oVjwcGBINUEgi05oyNj7+U=");
        String publicKey1 =("MFkwEwYHKoZIzj0CAQYIKoEcz1UBgi0DQgAEK2X3eSO4M3CSzlJfajjtlpqayQ0DQ0UfSLiAa6APukvaq3OgDnDYOfx5TeNUUGWGCKHJhBRZcB7Nccl/XCr55w==");
        /**
        * sm2验签   同步验签
         * 原通知内容：
         * {"msg":"success","biz_content":{"file_id":"/public/06/20240320/d58380aa24d98f5f27c37e8e42996741.jpeg"},"code":10000,"sign":"NWnjl7R1MVUAa+d86TIuJXzdumNcwpYLtTdjm1Kq1kjZbS5nWHWj9tXDPOHYrfcrZjbD2yOUfUVMuwvzq014Iw=="}
        */
        @Test
        public void Sm2verify1() throws Exception {
               String sign = "f1lkmW+zGmbNCNkka4EDWwyaN4/YQBSxKM+L/sH1YpnHpixkhrHZMXFUIxsWwCAwm4jaogAqc59l1XuJZlFo3w==";
                String waitStr = "app_id=hy20250207154518238161&apply_no=250514111502078236&customer_id=250514111502001536&heepay_account=13788773588139.com&heepay_id=2360319&notify_type=1&reject_reason=1";
                String strUserId = "1234567812345678";
                Boolean flag = Sm2Util.verifySm3WithSm2(waitStr, strUserId, sign, Sm2Util.string2PublicKey(publicKey1));
                System.out.println(flag);
        }


        /**
         * sm2签名
         *
         * @return
         */
        @Test
        public void Sm2Sign() throws Exception {
                String data = "app_id=hykj-sm2002&biz_content={\"region_type\":1,\"region_code\":\"0\"}&charset=utf-8&format=json&method=customer.region.query&notify_url=http://www.example.com/notify&sign_type=SM2&timestamp=2024-12-19 18:05:29&version=1.0";
                String strUserId = "1234567812345678";
                String  sign = String.valueOf(Sm2Util.signSm3WithSm2(data, strUserId, Sm2Util.string2PrivateKey(privateKey)));
                System.out.println(sign);
        }




        /**
         * json -> ASCII码从小到大排序（字典序），使用URL键值对的格式
         *
         */

        @Test
        public void JsonToAz(){
                JSONObject B = new JSONObject();
                B.put("notify_type", "1");
                B.put("heepay_account", "13788773588139.com");
                B.put("wallet_id", "");
                B.put("apply_no", "250514111502078236");
                B.put("reject_reason", "1");
                B.put("customer_id", "250514111502001536");
                B.put("app_id", "hy20250207154518238161");
                B.put("heepay_id", "2360319");

                String a = jsonDataSortAToZ(B);
                System.out.println(a);
        }
        /**
         * 参数A-Z排序
         *
         * @param jsonData json 数据
         * @return String
         */
        public static String jsonDataSortAToZ(JSONObject jsonData) {
                Map<String, Object> map = jsonData.getInnerMap();
                List<Map.Entry<String, Object>> list = new ArrayList<>(map.entrySet());
                //升序排序
                list.sort(Comparator.comparing(Map.Entry::getKey));
                StringBuilder sb = new StringBuilder();
                for (Map.Entry<String, Object> mapping : list) {
                        if (mapping.getValue() == null || "".equals(mapping.getValue())) {
                                continue;
                        }
                        if (sb.length() > 0) {
                                sb.append("&");
                        }
                        sb.append(mapping.getKey());
                        sb.append("=");
                        sb.append(mapping.getValue());
                }
                return sb.toString();
        }


}
